home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / MAPPLY.PY < prev    next >
Encoding:
Text File  |  1999-09-01  |  6.2 KB  |  162 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. """Provide an apply-like facility that works with any mapping object
  65. """
  66.  
  67. def default_call_object(object, args, context):
  68.     result=apply(object,args) # Type s<cr> to step into published object.
  69.     return result
  70.  
  71. def default_missing_name(name, context):
  72.     raise TypeError, 'argument %s was ommitted' % name
  73.  
  74. def default_handle_class(klass, context):
  75.     if hasattr(klass,'__init__'): 
  76.         f=klass.__init__.im_func
  77.         c=f.func_code
  78.         names=c.co_varnames[1:c.co_argcount]
  79.         return klass, names, f.func_defaults
  80.     else:
  81.         return klass, (), ()
  82.  
  83. def mapply(object, positional=(), keyword={},
  84.            debug=None, maybe=None,
  85.            missing_name=default_missing_name,
  86.            handle_class=default_handle_class,
  87.            context=None, bind=0,
  88.            ):
  89.  
  90.     if hasattr(object,'__bases__'):
  91.         f, names, defaults = handle_class(object, context)
  92.     else:
  93.         f=object
  94.         im=0
  95.         if hasattr(f, 'im_func'):
  96.             im=1
  97.         elif not hasattr(f,'func_defaults'):
  98.             if hasattr(f, '__call__'):
  99.                 f=f.__call__
  100.                 if hasattr(f, 'im_func'):
  101.                     im=1
  102.                 elif not hasattr(f,'func_defaults') and maybe: return object
  103.             elif maybe: return object
  104.     
  105.         if im:
  106.             f=f.im_func
  107.             c=f.func_code
  108.             defaults=f.func_defaults
  109.             names=c.co_varnames[1:c.co_argcount]
  110.         else:
  111.             defaults=f.func_defaults
  112.             c=f.func_code
  113.             names=c.co_varnames[:c.co_argcount]
  114.  
  115.     nargs=len(names)
  116.     if positional:
  117.         positional=list(positional)
  118.         if bind and nargs and names[0]=='self':
  119.             positional.insert(0, missing_name('self', context))
  120.         if len(positional) > nargs: raise TypeError, 'too many arguments'
  121.         args=positional
  122.     else:
  123.         if bind and nargs and names[0]=='self':
  124.             args=[missing_name('self', context)]
  125.         else:
  126.             args=[]
  127.  
  128.     get=keyword.get
  129.     nrequired=len(names) - (len(defaults or ()))
  130.     for index in range(len(args), len(names)):
  131.         name=names[index]
  132.         v=get(name, args)
  133.         if v is args:
  134.             if index < nrequired: v=missing_name(name, context)
  135.             else: v=defaults[index-nrequired]
  136.         args.append(v)
  137.  
  138.     args=tuple(args)
  139.     if debug is not None: return debug(object,args,context)
  140.     else: return apply(object,args)
  141.